<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * @desc 基础模型
 */
class Base_model extends CI_Model{
    public $table = "";
    public $error = "";//操作数据库产生的错误信息
    public $primary_key = "";//主键
    public function __construct()
    {
        $this->load->database();
//        $this->db->query("set names utf8mb4");
    }

    /**
     * 添加数据
     */
    public function add($table,$data){
        $this->db->insert($table,$data);
        echo $this->db->last_query();
        return $this->db->insert_id();
    }

    /**
     * 修改数据
     */
    public function update($table,$data,$where){
        return $this->db->update($table,$data,$where);
    }

    /**
     * 删除数据
     */
    public function dels($table,$where){
        return $this->db->delete($table,$where);
    }

    /**
     * 查询一条数据
     */
    public function one($table,$where,$select=""){
        if(empty($select)){
            $this->db->select('*');
        }else{
            $this->db->select($select);
        }
        $this->db->from($table);
        $this->db->where($where);
        $res = $this->db->get();
        $datas= $res->row_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 查询所有
     */
    public function alls($table,$where=[],$select=1,$order_by=[]){
        if($select==1){
            $this->db->select('*');
        }else{
            $this->db->select($select);
        }
        $this->db->from($table);
        if(!empty($where)){
            $this->db->where($where);
        }
        if(!empty($order_by)){
            foreach($order_by as $key=>$value){
                $this->db->order_by($key,$value);
            }
        }
        $res = $this->db->get();
        $datas['row']= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * @desc 单表查询
     * @param array $config 配置
     * @return array
     */
    public function singleTableList(array $config=[])
    {
        $data = ["total"=>0,"rows"=>[]];
        if(!empty($config["select"])){
            $this->db->select($config["select"]);
        }
        $this->db->from($this->table);

        //需要过滤的
        if(!empty($config["eq-filter"])){
            //过滤空条件
            $config["eq-filter"] =array_filter($config["eq-filter"]);
            $this->db->where($config["eq-filter"]);
        }
        //不需要过滤的搜索
        if(!empty($config["eq"])){
            $this->db->where($config["eq"]);
        }
        if(!empty($config["search"])){
            if(is_array($config["search"])){
                $config["search"] = array_filter($config["search"]);
                $like = array ();
                foreach($config["search"] as $k=>$v){
                    array_push($like,"{$k} like '%{$v}%'") ;
                }
                if($like){
                    $this->db->where("(".implode(" or ",$like).")");
                }
            }
        }
        if(!empty($config["like"])){
            $config["like"] = array_filter($config["like"]);
            foreach($config["like"] as $key=>$val){
                $this->db->like($key,"{$val}");
            }
        }
        if(!empty($config["where_in"])){
            $config["where_in"] = array_filter($config["where_in"]);
            foreach($config["where_in"] as $key=>$val){
                $this->db->where_in($key,$val);
            }
        }
        if(!empty($config["group"])){
            //多个group by用逗号分割
            $this->db->group_by($config["group"]);
        }
        if(!empty($config["page"])){
            //分页
            $pageSize = $config["page"]["limit"] ? $config["page"]["limit"] : 20;
            $data["total"] = $this->db->count_all_results("",false);
            $this->db->limit($pageSize,($config["page"]["page"]-1)*$pageSize);
        }
        if(!empty($config["order"])){
            foreach($config["order"] as $key=>$value){
                $this->db->order_by($key,$value);
            }
        }
        $query = $this->db->get();
//        echo $this->db->last_query()."<br>";
        $data["rows"] = $query->result_array();
//        echo $this->db->last_query();exit;
        return $data;
    }
    /**
     * @desc 单表查询 总数
     * @param array $config 配置
     */
    public function singleTableCount(array $config=[])
    {
        if(!empty($config["select"])){
            $this->db->select($config["select"]);
        }
        $this->db->from($this->table);

        //需要过滤的
        if(!empty($config["eq-filter"])){
            //过滤空条件
            $config["eq-filter"] =array_filter($config["eq-filter"]);
            $this->db->where($config["eq-filter"]);
        }
        //不需要过滤的搜索
        if(!empty($config["eq"])){
            $this->db->where($config["eq"]);
        }
        if(!empty($config["like"])){
            $config["like"] = array_filter($config["like"]);
            $likes = [];
            foreach($config["like"] as $key=>$val){
                $likeWhere = "{$key} like '%{$val}%'";
                array_push($likes,$likeWhere);
            }
            if($likes){
                $likestr = implode(" or ",$likes);
                $this->db->where("(".$likestr.")");
            }
        }
        if(!empty($config["group"])){
            //多个group by用逗号分割
            $this->db->group_by($config["group"]);
        }
        return  $this->db->count_all_results();

    }

    /**
     * @desc 通过主键id获取信息
     * @param $primaryId 主键id
     */
    public function getInfoByPrimaryId($primaryId,$select="*")
    {
        $this->db->select($select);
        $this->db->from($this->table);
        $this->db->where(["{$this->primary_key}"=>$primaryId]);
        $query = $this->db->get();
        $result = $query->row_array();
        return $result;
    }

    /**
     * @desc 获取总量
     * @param array $where
     * @return mixed
     */
    public function getTotal(array $where=[])
    {
        $this->db->select("1");
        $this->db->from($this->table);
        if($where){
            $this->db->where($where);
        }
       return  $this->db->count_all_results();
    }

    /**
     * @desc 批量获取数据信息，通过主键值
     * @param array $value
     */
    public function getInfoByPrimaryWhereIn(array $value,$select = "*")
    {
        $this->db->select($select);
        $this->db->from($this->table);
        $this->db->where_in($this->primary_key,$value);
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * @Desc 通过条件获取单表信息
     * @param array $where
     * @param string $select
     */
    public function getInfo(array $where,$onlyOne=true,$select="*")
    {
        $this->db->select($select);
        $this->db->from($this->table);
        $this->db->where($where);
        $query = $this->db->get();
        if($onlyOne){
            return $query->row_array();
        }else{
            return $query->result_array();
        }
    }
    public function limit($pageSize,$page)
    {
        $this->db->limit($pageSize,($page-1)*$pageSize);
    }
    public function delete($where)
    {
        foreach ($where as $k=>$v){
            is_array($v) ? $this->db->where_in($k,$v) : $this->db->where($k,$v);
        }
        return $this->db->delete($this->table);
    }

    /**
     * 查询所有用户
     */
    public function users($table,$select="",$type=1){
        if(empty($select)){
            $this->db->select("*");
        }else{
            $this->db->select($select);
        }
        $this->db->from($table);
        if($type==2){
            $this->db->where("status",2);
        }

        $query = $this->db->get();
        return $query->result_array();
    }
}